package com.koron.css2.clientCenter.impl;

import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.AccountInfoBean;
import com.koron.css2.clientCenter.bean.CtmInfoBean;
import com.koron.css2.clientCenter.mapper.AccountInfoMapper;
import com.koron.css2.clientCenter.mapper.CtmInfoMapper;
import com.koron.css2.clientCenter.queryBean.UserInfoSplitCheckQuery;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.stream.Collectors;

public class UserInfoSplitCheckList implements ServerInterface {
    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
        try {
            UserInfoSplitCheckQuery userInfoSplitCheckQuery = JsonUtils.objectToPojo(req.getData(), UserInfoSplitCheckQuery.class);
            CtmInfoMapper ctmInfoMapper = factory.getMapper(CtmInfoMapper.class);
            AccountInfoMapper accountInfoMapper = factory.getMapper(AccountInfoMapper.class);

            String ctmResult = "";
            String accountResult = "";

            // 1. 查出对应的所有客户
            List<String> ctmNoList = userInfoSplitCheckQuery.getCtmNoList();
            List<CtmInfoBean> ctmInfoBeanList = ctmInfoMapper.getListByCtmNoList(ctmNoList, CtmInfoBean.CTM);
            // 2. 判断是否有客户不存在
            if (ctmNoList.size() != ctmInfoBeanList.size()) {
                // 2.1 筛选出现存的所有客户编号
                List<String> existCtmNoList = ctmInfoBeanList.stream().map(CtmInfoBean::getCtmNo).collect(Collectors.toList());
                // 2.2 筛选出查不出数据的客户编号
                ctmNoList.removeAll(existCtmNoList);
                // 2.3 将结果以“，”相联结
                ctmResult = ctmNoList.stream().collect(Collectors.joining(","));
            }
            // 3. 查出对应的所有账户
            List<String> accountNoList = userInfoSplitCheckQuery.getAccountNoList();
            List<AccountInfoBean> accountInfoBeanList = accountInfoMapper.getListByAccountNoList(accountNoList, AccountInfoBean.ACCOUNT);
            // 4. 判断是否有账户不存在
            if (accountNoList.size() != accountInfoBeanList.size()) {
                // 4.1 筛选出现存的所有账户编号
                List<String> existAccountNoList = accountInfoBeanList.stream().map(AccountInfoBean::getAccountNo).collect(Collectors.toList());
                // 4.2 筛选出查不出数据的账户编号
                accountNoList.removeAll(existAccountNoList);
                // 4.3 将结果以“，”相联结
                accountResult = accountNoList.stream().collect(Collectors.joining(","));
            }
            // 5. 如果一切正常
            if (StringUtils.isEmpty(ctmResult) && StringUtils.isEmpty(accountResult)) {
                return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "用户可以还原", null);
            } // 6. 如果有账户/客户不存在
            else {
                return MessageBean.create(Constant.MESSAGE_INT_FAIL, ctmResult + "," + accountResult, null);
            }
        } catch (Exception e) {
            logger.error("还原确认失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "还原确认失败", null);
        }
    }
}
